package com.devicemantra.coordinate;

import com.devicemantra.coordinate.dataobjects.*;

import java.util.*;
import java.io.IOException;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import com.devicemantra.coordinate.ds.tests.*;
import com.google.appengine.api.datastore.Blob;

@SuppressWarnings("serial")
public class getImage extends HttpServlet 
{
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		
			boolean lDoesExist = true;
			Logo lLogo = null;
			String lImageKey = req.getParameter("image_key");
			System.out.println("Request for Image with key " + lImageKey);
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try
			{
				lLogo = pm.getObjectById(Logo.class,lImageKey);
				lLogo = pm.detachCopy(lLogo);
				
				
			} catch(JDOObjectNotFoundException e)
			{
				lDoesExist = false;
				
			}
			pm.close();
			
			if(lDoesExist)
			{
				System.out.println("[ FOUND ]");
				
			}
			else
			{
				System.out.println("[ NOT FOUND ]");
			}
			
			
			if( lDoesExist && (lLogo != null) )
			{
				resp.setContentType(lLogo.getContentType());
				Blob lBlob = lLogo.getImage();
				resp.getOutputStream().write(lBlob.getBytes());
			}
			
			
	}
}
